package com.nineclock.message.service.impl;


import cn.hutool.core.util.StrUtil;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.message.dto.NcMessageDto;
import com.nineclock.message.dto.NcMessageQueryDTO;
import com.nineclock.message.enums.MessageTypeEnum;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.NcMessageService;
import com.nineclock.message.utils.JPushManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NcMessageServiceImpl implements NcMessageService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private JPushManager pushManager; //自定义工具类

    /**
     * 推送消息
     *
     * @param ncMessage
     */
    @Override
    public void pushMessage(NcMessage ncMessage) {

        //封装NcMessage属性
        String title = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getTitle();
        String audience = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getAudience();

        ncMessage.setTitle(title);
        ncMessage.setAudience(audience);

        //将消息保存在MongoDB
        mongoTemplate.save(ncMessage);

        //推送消息
        try {
            //根据别名推送
            pushManager.sendNotificationByAlias(title, ncMessage.getContent(), ncMessage.getTargets(), null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据条件查询消息列表
     *
     * @param queryDTO
     * @return
     */
    @Override
    public List<NcMessageDto> queryMessage(NcMessageQueryDTO queryDTO) {

        //组装查询条件(mangoDB)
        Query query = new Query();

        if (queryDTO == null) {
            queryDTO = new NcMessageQueryDTO();
        }

        if (StrUtil.isNotEmpty(queryDTO.getId())) {
            query.addCriteria(Criteria.where("_id").is(queryDTO.getId())); // where id = ?
        }
        if (StrUtil.isNotEmpty(queryDTO.getType())) {
            query.addCriteria(Criteria.where("messageType").is(queryDTO.getType()));
        }
        if (queryDTO.getTargetId() != null) {
            query.addCriteria(Criteria.where("approveUserId").is(queryDTO.getTargetId()));
        }
        if (queryDTO.getUserId() != null) {
            query.addCriteria(Criteria.where("applyUserId").is(queryDTO.getUserId()));
        }

        //查询
        List<NcMessage> messageDtos = mongoTemplate.find(query, NcMessage.class);

        //返回数据
        return BeanHelper.copyWithCollection(messageDtos,NcMessageDto.class);
    }

    /**
     * 更新消息状态为通过(MDB)
     * @param messageId
     */
    @Override
    public void updateMessageWithApproved(String messageId) {

        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(messageId)));

        Update update = new Update();
        update.set("useStatus",1);
        update.set("approveStatue",1);

        mongoTemplate.updateFirst(query, update, NcMessage.class);
    }
}
